package com.sh.data.engine.job.admin.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.data.engine.job.admin.core.model.XxlJobInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * job info
 *
 * @author xuxueli 2016-1-12 18:03:45
 */
@Mapper
public interface XxlJobInfoDao extends BaseMapper<XxlJobInfo> {

    default List<XxlJobInfo> pageList(
        int offset,
        int pagesize,
        int jobGroup,
        int triggerStatus,
        String jobDesc,
        String executorHandler,
        String author) {
        LambdaQueryWrapper<XxlJobInfo> wrapper = new QueryWrapper<XxlJobInfo>().lambda();
        if (jobGroup > 0) {
            wrapper.eq(XxlJobInfo::getJobGroup, jobGroup);
        }
        if (triggerStatus >= 0) {
            wrapper.eq(XxlJobInfo::getTriggerStatus, triggerStatus);
        }
        if (StringUtils.isNotBlank(jobDesc)) {
            wrapper.like(XxlJobInfo::getJobDesc, jobDesc);
        }
        if (StringUtils.isNotBlank(executorHandler)) {
            wrapper.like(XxlJobInfo::getExecutorHandler, executorHandler);
        }
        if (StringUtils.isNotBlank(author)) {
            wrapper.like(XxlJobInfo::getAuthor, author);
        }
        Page<XxlJobInfo> xxlJobInfoPage = selectPage(new Page<XxlJobInfo>(offset, pagesize), wrapper);
        List<XxlJobInfo> records = xxlJobInfoPage.getRecords();
        return records;
    }

    default int pageListCount(
        int offset,
        int pagesize,
        int jobGroup,
        int triggerStatus,
        String jobDesc,
        String executorHandler,
        String author) {
        LambdaQueryWrapper<XxlJobInfo> wrapper = new QueryWrapper<XxlJobInfo>().lambda();
        wrapper
            .eq(jobGroup > 0, XxlJobInfo::getJobGroup, jobGroup)
            .eq(triggerStatus >= 0, XxlJobInfo::getTriggerStatus, triggerStatus)
            .like(StringUtils.isNotBlank(jobDesc), XxlJobInfo::getJobDesc, jobDesc)
            .like(
                StringUtils.isNotBlank(executorHandler),
                XxlJobInfo::getExecutorHandler,
                executorHandler)
            .like(StringUtils.isNotBlank(author), XxlJobInfo::getAuthor, author);
        return Math.toIntExact(this.selectCount(wrapper));
    }

    default int save(XxlJobInfo info) {

        return this.insert(info);
    }

    default XxlJobInfo loadById(int id) {
        return this.selectById(id);
    }

    default int update(XxlJobInfo xxlJobInfo) {
        return this.updateById(xxlJobInfo);
    }

    default int delete(long id) {
        return this.deleteById(id);
    }

    default List<XxlJobInfo> getJobsByGroup(int jobGroup) {
        LambdaQueryWrapper<XxlJobInfo> wrapper = new QueryWrapper<XxlJobInfo>().lambda();
        wrapper.eq(XxlJobInfo::getJobGroup, jobGroup);
        return this.selectList(wrapper);
    }

    default int findAllCount() {
        return Math.toIntExact(this.selectCount(new LambdaQueryWrapper<XxlJobInfo>()));
    }

    default List<XxlJobInfo> scheduleJobQuery(long maxNextTime, int pagesize) {
        LambdaQueryWrapper<XxlJobInfo> wrapper = new QueryWrapper<XxlJobInfo>().lambda();
        wrapper
            .eq(XxlJobInfo::getTriggerStatus, 1)
            .le(XxlJobInfo::getTriggerNextTime, maxNextTime)
            .orderByAsc(XxlJobInfo::getId)
            .last(String.format("LIMIT %d", pagesize));
        return this.selectList(wrapper);
    }

    default int scheduleUpdate(XxlJobInfo xxlJobInfo) {
        return this.updateById(xxlJobInfo);
    }
}
